<?php
Globals::requireClass('Table');
Globals::requireTable('Group');
Globals::requireTable('AclResource');

class AclTable extends Table
{
	public static $defaultConfig = array(
		'table' => 'admin_acl'
	);
	
	protected $groupTable;
	protected $aclResourceTable;
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		
		$this->groupTable		= new GroupTable($config);
		$this->aclResourceTable = new AclResourceTable($config);
	}
	
	public function validate(array &$data)
	{
		if (array_key_exists($key = 'groupid', $data))
		{
			$data[$key]	= intval($data[$key]);
			$row		= $this->groupTable->getRow($data[$key]);
			
			if (!$row)
			{
				$this->message = '群组ID无效。';
				return false;
			}
		}
		
		if (array_key_exists($key = 'resourceid', $data))
		{
			$data[$key]	= intval($data[$key]);
			$row		= $this->aclResourceTable->getRow($data[$key]);
			
			if (!$row)
			{
				$this->message = '权限对象ID无效。';
				return false;
			}
		}
		
		return true;
	}
}

Config::extend('AclTable', 'Table');
